package java_thinking.unit_21._21_3;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

/**
 * @Desc:   3-3 线程安全的可变整数 加上synchronized才是
 *              测试类 concurrency.ConcurrencyTest#Test_3_3()
 *              无法保证线程中的操作将按照程序中的顺序来执行
 * @author: cww
 * @DateTime: 2019-05-30 20:59
 */
@ThreadSafe
public class SynchronizedInteger extends Thread{
    @GuardedBy("this") private int value;

    public synchronized int getValue() {
        return value;
    }

    public synchronized void setValue(int value) throws InterruptedException {
        this.value = value;
//        sleep(100);
    }

    @Override
    public void run() {
//        try {
//            sleep(100);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println(getValue() + "-0");
        System.out.println("线程");
        try {
            setValue(33);
            System.out.println("线程结束" + "-" + getValue());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

